package com.example.mooney.web;

import com.example.mooney.domain.Category;
import com.example.mooney.domain.Income;
import java.lang.Long;
import java.lang.String;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

privileged aspect IncomeController_Roo_Controller {
    
    @RequestMapping(value = "/income", method = RequestMethod.POST)    
    public String IncomeController.create(@ModelAttribute("income") Income income, BindingResult result, ModelMap modelMap) {    
        if (income == null) throw new IllegalArgumentException("A income is required");        
        for (ConstraintViolation<Income> constraint : Validation.buildDefaultValidatorFactory().getValidator().validate(income)) {        
            result.rejectValue(constraint.getPropertyPath().toString(), "income.error." + constraint.getPropertyPath(), constraint.getMessage());            
        }        
        if (result.hasErrors()) {        
            modelMap.addAllAttributes(result.getAllErrors());            
            modelMap.addAttribute("income", income);            
            modelMap.addAttribute("categorys", Category.findAllCategorys());            
            return "income/create";            
        }        
        income.persist();        
        return "redirect:/income/" + income.getId();        
    }    
    
    @RequestMapping(value = "/income/form", method = RequestMethod.GET)    
    public String IncomeController.createForm(ModelMap modelMap) {    
        modelMap.addAttribute("income", new Income());        
        modelMap.addAttribute("categorys", Category.findAllCategorys());        
        return "income/create";        
    }    
    
    @RequestMapping(value = "/income/{id}", method = RequestMethod.GET)    
    public String IncomeController.show(@PathVariable("id") Long id, ModelMap modelMap) {    
        if (id == null) throw new IllegalArgumentException("An Identifier is required");        
        modelMap.addAttribute("income", Income.findIncome(id));        
        return "income/show";        
    }    
    
    @RequestMapping(value = "/income", method = RequestMethod.GET)    
    public String IncomeController.list(@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, ModelMap modelMap) {    
        if (page != null || size != null) {        
            int sizeNo = size == null ? 10 : size.intValue();            
            modelMap.addAttribute("incomes", Income.findIncomeEntries(page == null ? 0 : (page.intValue() - 1) * sizeNo, sizeNo));            
            float nrOfPages = (float) Income.countIncomes() / sizeNo;            
            modelMap.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));            
        } else {        
            modelMap.addAttribute("incomes", Income.findAllIncomes());            
        }        
        return "income/list";        
    }    
    
    @RequestMapping(method = RequestMethod.PUT)    
    public String IncomeController.update(@ModelAttribute("income") Income income, BindingResult result, ModelMap modelMap) {    
        if (income == null) throw new IllegalArgumentException("A income is required");        
        for (ConstraintViolation<Income> constraint : Validation.buildDefaultValidatorFactory().getValidator().validate(income)) {        
            result.rejectValue(constraint.getPropertyPath().toString(), "income.error." + constraint.getPropertyPath(), constraint.getMessage());            
        }        
        if (result.hasErrors()) {        
            modelMap.addAllAttributes(result.getAllErrors());            
            modelMap.addAttribute("income", income);            
            modelMap.addAttribute("categorys", Category.findAllCategorys());            
            return "income/update";            
        }        
        income.merge();        
        return "redirect:/income/" + income.getId();        
    }    
    
    @RequestMapping(value = "/income/{id}/form", method = RequestMethod.GET)    
    public String IncomeController.updateForm(@PathVariable("id") Long id, ModelMap modelMap) {    
        if (id == null) throw new IllegalArgumentException("An Identifier is required");        
        modelMap.addAttribute("income", Income.findIncome(id));        
        modelMap.addAttribute("categorys", Category.findAllCategorys());        
        return "income/update";        
    }    
    
    @RequestMapping(value = "/income/{id}", method = RequestMethod.DELETE)    
    public String IncomeController.delete(@PathVariable("id") Long id) {    
        if (id == null) throw new IllegalArgumentException("An Identifier is required");        
        Income.findIncome(id).remove();        
        return "redirect:/income";        
    }    
    
    @InitBinder    
    public void IncomeController.initBinder(WebDataBinder binder) {    
        binder.registerCustomEditor(java.util.Date.class, new org.springframework.beans.propertyeditors.CustomDateEditor(new java.text.SimpleDateFormat("M/d/yy"), true));        
    }    
    
}
